package gggameserver.util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class GGConfiguration {

	private static org.apache.log4j.Logger log = Logger
			.getLogger(GGConfiguration.class);

	private static GGConfiguration instance;

	private Properties props = null;

	private GGConfiguration() throws FileNotFoundException, IOException {
		props = new Properties();
		props.load(new FileReader("gggameserver.ini"));
	}

	public static synchronized GGConfiguration getInstance()
			throws FileNotFoundException, IOException {
		if (null == instance)
			instance = new GGConfiguration();
		return instance;
	}

	public static boolean getBooleanProperty(String key) {
		String value = getProperty(key);

		if (null == value)
			return false;

		if (value.equalsIgnoreCase("true"))
			return true;
		if (value.equalsIgnoreCase("1"))
			return true;
		if (value.equalsIgnoreCase("y"))
			return true;
		if (value.equalsIgnoreCase("yes"))
			return true;

		if (value.equalsIgnoreCase("false"))
			return true;
		if (value.equalsIgnoreCase("0"))
			return true;
		if (value.equalsIgnoreCase("n"))
			return true;
		if (value.equalsIgnoreCase("no"))
			return true;

		log.warn("Illegal boolean parameter '" + key + "' = '" + value + "'");
		return false;
	}

	public static long getLongProperty(String key, int defaultValue) {
		String value = getProperty(key, "" + defaultValue);

		try {
			long result = Long.parseLong(value);
			return result;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			log.warn("Illegal long value ('" + getProperty(key)
					+ "') for key '" + key + "' ");
			throw e;
		}
	}

	public static String getProperty(String key) {
		try {
			String value = getInstance().getProperty_(key);
			if (null == value)
				log.warn("Missing property '" + key + "'");
			return value;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String getProperty(String key, String defaultValue) {
		try {
			String value = getInstance().getProperty_(key, defaultValue);
			return value;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public String getProperty_(String key) {
		String value = this.props.getProperty(key);
		// if (null == value) log.warn("Missing property '" + key + "'");
		return value;
	}

	public String getProperty_(String key, String defaultValue) {
		String value = this.props.getProperty(key, defaultValue);
		// if (null == value) log.warn("Missing property '" + key + "'");
		return value;
	}

	private static boolean static_calculated = false;
	private static boolean iswindows = true;

	public static synchronized void calculateStatic() {
		if (static_calculated)
			return;
		iswindows = System.getProperty("os.name").toLowerCase()
				.startsWith("windows");
		static_calculated = true;
	}

	public static boolean isWindows() {
		// if (!static_calculated)
		calculateStatic();
		return iswindows;
	}

}
